import * as types from './mutation-types';
import message from '../utils/message'
import { toMenu } from '../utils/listToTree'
import axios from 'axios'
// 登录用户信息
export const updateUserInfo = async ({commit}, data) => {
  commit (types.SET_USERINFO, data);
};

// token
export const updateToken = async ({commit}, data) => {
  commit (types.SET_TOKEN, data);
  let access_ = data
      if (access_) {
        axios.defaults.headers['Authorization'] = access_
      }
};

// token
export const delToken = async ({commit}, data) => {
  commit (types.DEL_TOKEN, data);
  axios.defaults.headers['Authorization'] = ''
};

// 屏幕宽度
export const updateScreenWidth = async ({commit}, data) => {
  commit (types.SET_WIDTH, data);
};

// 屏幕高度
export const updateScreenHeight = async ({commit}, data) => {
  commit (types.SET_HEIGHT, data);
};

// 权限菜单路由按钮
export const updateSup = async ({commit}, sub) => {
  let sups = toMenu(sub)
      let subList = sups.subList
      let btns = sups.btnList
      let permissionRouter = sups.permissionRouter
      if (subList.length === 0) {
        message.toastOnce('该角色暂无配置菜单')
      }
      await commit(types.SET_PERMISSIONROUTER, permissionRouter)
      await commit(types.SET_SUB, subList)
      await commit(types.SET_BTN, btns)
};

// 动态路由
export const updatePermissionRouter = async ({ commit }, permissionRouter) => {
  commit(types.SET_PERMISSIONROUTER, permissionRouter)
}


export const delPermissionRouter = async ({ commit }, permissionRouter) => {
  commit(types.DEL_PERMISSIONROUTER, permissionRouter)
}

// 左侧title名称
export const setRouter = async ({ commit }, routeName) => {
  commit(types.SET_ROUTER, routeName)
}